#include "ap3216.h"
#include "ui_ap3216.h"
#include <QValueAxis>
#include <QSplineSeries> // 曲线
#include <QLineSeries> // 折线
#include "control.h"
#include "adc.h"


static int zero_flag=0;

static int ALS_Y_play,ALS_Y_ADD;

ap3216::ap3216(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ap3216)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
}

ap3216::~ap3216()
{
    delete ui;
}

void ap3216::ap3216c_ALS_play()
{

    float ALS=0;
    int IR=0;
    int PS=0;

    static float buff_ALS[500];
    static int  buff_ALS_p = 0;
    static int x,i;


    // 2.创建一个图表
    QChart *ALS_CHART = new QChart();
    // 3.创建坐标轴
    QValueAxis *ALS_X = new QValueAxis();
    QValueAxis *ALS_Y = new QValueAxis();

    //显示数值

    char displayall[100];
    const char * buff;


    char* data = NULL;
    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        extern QByteArray getcmd;
        buff = QString(getcmd).toStdString().c_str();
    }
    extern int tcp_state;
    if(tcp_state)
    {
        extern QByteArray TCP_buff;
        buff = QString(TCP_buff).toStdString().c_str();
    }


    data = strstr(buff,"ALS");
    if(data != NULL)
    {
        data+=3;
        ALS = atof(data);

    }

    data = strstr(buff,"IR");
    if(data != NULL)
    {
        data+=3;
        IR =  atoi(data) ;

    }
    data = strstr(buff,"PS");
    if(data != NULL)
    {
        data+=2;
        PS = atoi(data) ;
    }


    if((ALS == 0) && (IR == 0) && (PS == 0))
    {
        zero_flag=1;

    }
    else
    {
        zero_flag=0;

    }

    if(strcmp(buff,"@ap3216cOK .") && (!zero_flag))
    {

        sprintf(displayall," ALS:%.02lf",ALS);
        ui->textBrowser_4->setText(displayall);

      buff_ALS[buff_ALS_p] = ALS;
      buff_ALS_p++;


      // 4.设置坐标轴的范围
      if(x>=1000)
      {
          ALS_X->setRange(x, 1000+x);
      }
      else
      {
          ALS_X->setRange(0, 1000);
      }

      if(ALS_Y_play==0)
      {
          if(buff_ALS[buff_ALS_p-1] < 5)
          {
              ALS_Y->setRange(0, 10);
          }
          else if((buff_ALS[buff_ALS_p-1] > 10000)  && (buff_ALS[buff_ALS_p-1] < 15000))
          {
              ALS_Y->setRange(0, 15000);
          }
          else if(buff_ALS[buff_ALS_p-1] > 15000)
          {
              ALS_Y->setRange(0, 25000);
          }
          else
          {
               ALS_Y->setRange(0, 2*buff_ALS[buff_ALS_p-1]);
          }
      }
      else if(ALS_Y_play>=1)
      {


             ALS_Y->setRange(0, 1000+ALS_Y_ADD);

      }


     buff_left_float(&buff_ALS_p, buff_ALS);



      // 5.设置坐标轴的标题和显示的格式
      ALS_X->setTitleText("时间/ms");
      ALS_Y->setTitleText("采样值");
      ALS_X->setLabelFormat("%d");
      ALS_Y->setLabelFormat("%d");

      // 设置标题的字体大小
      QFont font;
      font.setPointSize(16); // 设置为16号字体
      // 如果你想要设置字体为粗体
      font.setBold(true);

      ALS_X->setLabelsFont(font);
      ALS_Y->setLabelsFont(font);



      // 设置xy的显示的格式
        ALS_X->setTickCount(6);
        ALS_Y->setTickCount(5);

      // 6.图表添加坐标轴
      ALS_CHART->createDefaultAxes();
      ALS_CHART->addAxis(ALS_X, Qt::AlignBottom);
      ALS_CHART->addAxis(ALS_Y, Qt::AlignLeft);

      // 7.设置图表的标题以及图例显示是否需要
//      ALS_CHART->setTitle("采样值与时间曲线");
//      ALS_CHART->setTitleFont(font);
      ALS_CHART->legend()->setVisible(false);

      // 8.创建曲线对象添加它的点，设置曲线的颜色
      QLineSeries *lineSeries =  new QLineSeries();
      if(x>=1000)
      {
          qDebug() << x << endl;
          for(i=0;i<100;i++)
          {
              lineSeries->append(10*i+x,  buff_ALS[i]);
          }
      }
      else
      {

          for(i=0;i<100;i++)
          {
              lineSeries->append(10*i,  buff_ALS[i]);
          }
      }


      QPen pen(QColor(0xFF7F00),5);
      lineSeries->setPen(pen);

      // 9.图表添加曲线
      ALS_CHART->addSeries(lineSeries);

       ALS_CHART->setMargins({1, 1, 1, 1});
      // 10.!!!!将曲线的数据与坐标轴相连！！！！注意，这个要在图表添加曲线之后
      // 附属到坐标轴上面去
      lineSeries->attachAxis(ALS_X);
      lineSeries->attachAxis(ALS_Y);

      // 11.将图表放置于图表视图
      ui->ALS_Chart->setChart(ALS_CHART);





    x+=10;

    }


}

void ap3216::ap3216c_IR_play()
{

    int PS=0;


    static uint32_t buff_PS[400];
    static int  buff_PS_p = 0;
    static int x,i;


    // 2.创建一个图表
    QChart *PS_CHART = new QChart();
    // 3.创建坐标轴
    QValueAxis *PS_X = new QValueAxis();
    QValueAxis *PS_Y = new QValueAxis();

    //显示数值

    char* data = NULL;
    char displayall[10];
    const char *buff;

    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        extern QByteArray getcmd;
        buff = QString(getcmd).toStdString().c_str();
    }
    extern int tcp_state;
    if(tcp_state)
    {
        extern QByteArray TCP_buff;
        buff = QString(TCP_buff).toStdString().c_str();
    }


    if(strcmp(buff,"@ap3216cOK .") && (!zero_flag))
    {


         data = strstr(buff,"PS");
         if(data != NULL)
         {
             data+=3;
             PS =  atoi(data) ;

         }
         sprintf(displayall,"PS:%04d",PS);
         ui->textBrowser_2->setText(displayall);


      buff_PS[buff_PS_p] = PS;
      buff_PS_p++;

      // 4.设置坐标轴的范围
      if(x>=1000)
      {
          PS_X->setRange(x, 1000+x);
      }
      else
      {
          PS_X->setRange(0, 1000);
      }
      PS_Y->setRange(0, 1000);


//     buff_left_mov(&buff_IR_p, &buff_IR, 1);

     buff_left_float(&buff_PS_p, (float*)buff_PS);



      // 5.设置坐标轴的标题和显示的格式
      PS_X->setTitleText("时间/ms");
      PS_Y->setTitleText("采样值");
      PS_X->setLabelFormat("%d");
      PS_Y->setLabelFormat("%d");

      // 设置标题的字体大小
      QFont font;
      font.setPointSize(16); // 设置为16号字体
      // 如果你想要设置字体为粗体
      font.setBold(true);

      PS_X->setLabelsFont(font);
      PS_Y->setLabelsFont(font);



      // 设置xy的显示的格式
        PS_X->setTickCount(6);
        PS_Y->setTickCount(5);

      // 6.图表添加坐标轴
      PS_CHART->createDefaultAxes();
      PS_CHART->addAxis(PS_X, Qt::AlignBottom);
      PS_CHART->addAxis(PS_Y, Qt::AlignLeft);

      // 7.设置图表的标题以及图例显示是否需要

      PS_CHART->legend()->setVisible(false);

      // 8.创建曲线对象添加它的点，设置曲线的颜色
      QLineSeries *lineSeries =  new QLineSeries();
      if(x>=1000)
      {
          qDebug() << x << endl;
          for(i=0;i<100;i++)
          {
              lineSeries->append(10*i+x,  buff_PS[i]);
          }
      }
      else
      {

          for(i=0;i<100;i++)
          {
              lineSeries->append(10*i,  buff_PS[i]);
          }
      }


      QPen pen(QColor(0x8A2BE2), 5);
      lineSeries->setPen(pen);

      // 9.图表添加曲线
      PS_CHART->addSeries(lineSeries);
      // 10.!!!!将曲线的数据与坐标轴相连！！！！注意，这个要在图表添加曲线之后
      // 附属到坐标轴上面去
      lineSeries->attachAxis(PS_X);
      lineSeries->attachAxis(PS_Y);

      // 11.将图表放置于图表视图
      ui->PS_Chart->setChart(PS_CHART);
      PS_CHART->setMargins({1, 1, 1, 1});




    x+=10;

    }
}




void ap3216::ap3216c_PS_play()
{
    int IR=0;


    static uint32_t buff_IR[400];
    static int  buff_IR_p = 0;
    static int x,i;


    // 2.创建一个图表
    QChart *IR_CHART = new QChart();
    // 3.创建坐标轴
    QValueAxis *IR_X = new QValueAxis();
    QValueAxis *IR_Y = new QValueAxis();

    //显示数值

    char* data = NULL;
    char displayall[100];



    const char *buff;

    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        extern QByteArray getcmd;
        buff = QString(getcmd).toStdString().c_str();
    }
    extern int tcp_state;
    if(tcp_state)
    {
        extern QByteArray TCP_buff;
        buff = QString(TCP_buff).toStdString().c_str();
    }



    if(strcmp(buff,"@ap3216cOK .") && (!zero_flag))
    {


         data = strstr(buff,"IR");
         if(data != NULL)
         {
             data+=3;
             IR =  atoi(data) ;

         }

         sprintf(displayall," IR:%d",IR);
         ui->textBrowser_3->setText(displayall);




      buff_IR[buff_IR_p] = IR;
      buff_IR_p++;






      // 4.设置坐标轴的范围
      if(x>=1000)
      {
          IR_X->setRange(x, 1000+x);
      }
      else
      {
          IR_X->setRange(0, 1000);
      }
      IR_Y->setRange(0, 10);


//     buff_left_mov(&buff_IR_p, &buff_IR, 1);

     buff_left_float(&buff_IR_p, (float*)buff_IR);



      // 5.设置坐标轴的标题和显示的格式
      IR_X->setTitleText("时间/ms");
      IR_Y->setTitleText("采样值");
      IR_X->setLabelFormat("%d");
      IR_Y->setLabelFormat("%d");

      // 设置标题的字体大小
      QFont font;
      font.setPointSize(16); // 设置为16号字体
      // 如果你想要设置字体为粗体
      font.setBold(true);

      IR_X->setLabelsFont(font);
      IR_Y->setLabelsFont(font);



      // 设置xy的显示的格式
        IR_X->setTickCount(6);
        IR_Y->setTickCount(5);
      // 6.图表添加坐标轴
      IR_CHART->createDefaultAxes();
      IR_CHART->addAxis(IR_X, Qt::AlignBottom);
      IR_CHART->addAxis(IR_Y, Qt::AlignLeft);

      // 7.设置图表的标题以及图例显示是否需要

      IR_CHART->legend()->setVisible(false);

      // 8.创建曲线对象添加它的点，设置曲线的颜色
      QLineSeries *lineSeries =  new QLineSeries();
      if(x>=1000)
      {
          qDebug() << x << endl;
          for(i=0;i<100;i++)
          {
              lineSeries->append(10*i+x,  buff_IR[i]);
          }
      }
      else
      {

          for(i=0;i<100;i++)
          {
              lineSeries->append(10*i,  buff_IR[i]);
          }
      }


      QPen pen(QColor(0x0000CD), 5);
      lineSeries->setPen(pen);

      // 9.图表添加曲线
      IR_CHART->addSeries(lineSeries);
      // 10.!!!!将曲线的数据与坐标轴相连！！！！注意，这个要在图表添加曲线之后
      // 附属到坐标轴上面去
      lineSeries->attachAxis(IR_X);
      lineSeries->attachAxis(IR_Y);

      // 11.将图表放置于图表视图
      ui->IR_Chart->setChart(IR_CHART);
      IR_CHART->setMargins({1, 1, 1, 1});




    x+=10;

    }
}

void ap3216::buff_left_float(int *buff_ADC_p, float buff_ADC[500])
{
    qDebug() << "XXX" << endl;
    if(*buff_ADC_p == 101)//0~8都有数
    {
        qDebug() << "YYY" << endl;

        *buff_ADC_p = 0;
        while(++(*buff_ADC_p))
        {
            buff_ADC[*buff_ADC_p-1]=buff_ADC[*buff_ADC_p];
            if(*buff_ADC_p==100)//0~7都有数
            {
                break;
            }
        }


        *buff_ADC_p=100;
    }

}


void ap3216::on_pushButton_2_clicked()
{
    ALS_Y_play ++;
    if(ALS_Y_play==2)
    {
        ALS_Y_ADD=1000;

    }
    if(ALS_Y_play==3)
    {
        ALS_Y_ADD=5000;

    }
    if(ALS_Y_play==4)
    {
        ALS_Y_ADD=10000;

    }
    if(ALS_Y_play==5)
    {
        ALS_Y_ADD=15000;

    }
    if(ALS_Y_play==6)
    {
        ALS_Y_ADD=25000;

    }
    if(ALS_Y_play==7)
    {
       ALS_Y_play=0;

    }
}

void ap3216::on_pushButton_3_clicked()
{
    if(ALS_Y_play > 0)
    {
        ALS_Y_play --;
    }

    if(ALS_Y_play==2)
    {
        ALS_Y_ADD=1000;

    }
    if(ALS_Y_play==3)
    {
        ALS_Y_ADD=5000;

    }
    if(ALS_Y_play==4)
    {
        ALS_Y_ADD=10000;

    }
    if(ALS_Y_play==5)
    {
        ALS_Y_ADD=15000;

    }
    if(ALS_Y_play==6)
    {
        ALS_Y_ADD=25000;

    }
    if(ALS_Y_play==7)
    {
       ALS_Y_play=0;

    }
}

void ap3216::on_pushButton_clicked()
{
    extern QSerialPort *serial;



    int i;
       for(i=0;i<5;i++)
       {

           if(serial->isOpen()==true)
           {
               serial->write("@");
               serial->write("close");
               serial->write(" ");
               serial->write(".");
           }

       }
       extern int tcp_state;
       extern QTcpSocket *qtcpsocket;
       if(tcp_state == 1)
       {
           qtcpsocket->write("@");
           qtcpsocket->write("close");
           qtcpsocket->write(" ");
           qtcpsocket->write(".");
       }
       this->close();
}
